package 字符串.middle;

import java.util.*;

/**
 * @author Lw中
 * @date 2020/8/26 9:42
 */
public class Mst1002 {

    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] chars = strs[i].toCharArray();
            String key = sorted(chars);
            if (!map.containsKey(key)) {
                map.put(key, new ArrayList<String>());
            }
            map.get(key).add(strs[i]);
        }
        return new ArrayList<List<String>>(map.values());
    }

    public static String sorted(char[] chars) {
        // 通过Arrays的方法对字符数组进行排序
        Arrays.sort(chars);
        // 返回String类型的key
        return Arrays.toString(chars);
    }

    public static void main(String[] args) {
        String[] str = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> lists = groupAnagrams(str);
        for (int i = 0; i < lists.size(); i++) {
            System.out.println(Arrays.toString(new List[]{lists.get(i)}));
        }
    }

}
